<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客后台专题模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class GetpageModel extends PublicModel{

	/**
	 * 博客专题数据操作
	 * @return [type] [description]
	 */
	Public function index(){
		$file = loadfile(C('page.id'));
		$pageurl = U('index', '', true);
		$indexpage = 12;
		$count = count($file);
		$page = new Page($count, $indexpage, $pageurl);
		$pnum = explode(',', $page->limit());
		$data = array();
		$i = -1;
		foreach ($file as $k => $v) {
			$i++;
			if ($i < $pnum[0] || $i >= $pnum[0] + $pnum[1]) continue;
			$data[] = unfile(C('page.data').$k.'.Mark');
		}
		$data = $this->getunPage($data);
		$redata['plist'] = $data;
		$redata['page'] = $page->view();
		return $redata;
	}

	/**
	 * 处理删除与排序
	 * @param boolean $data  [description]
	 * @param boolean $check [description]
	 */
	Public function UpdateAndDel($data = false, $check = false){
		$file = loadfile(C('page.id'));
		if ($check) {
			foreach ($file as $k => $v) {
				foreach ($data as $z => $a) {
					if ($k == $z) {
						$file[$k]['ord'] = $a;
					}
				}
			}
			foreach ($data as $k => $v) {
				$serfile = unfile(C('page.data').$k.'.Mark');
				$serfile['ord'] = $v;
				if (!serfile(C('page.data').$k.'.Mark', $serfile)) {
					return false;
				}
			}
		} else {
			$hits = loadfile(C('hits'));
			foreach ($file as $k => $v) {
				foreach ($data as $z) {
					if ($k == $z) {
						unset($file[$z]);
						unset($hits[$z]);
						$pluanfileid = loadfile(C('pinglun.id').$z.'.php');
						if (!empty($pluanfileid)) {
							foreach ($pluanfileid as $k => $v) {
								unlink(C('pinglun.data').$k.'.Mark');
							}
						}
						unlink(C('pinglun.id').$z.'.php');
						unlink(C('page.data').$z.'.Mark');
					}
				}
			}
			uasort($hits, 'hits_sort');
			varfile(C('hits'), $hits);
		}
		uasort($file, 'List_Sort');
		if (varfile(C('page.id'), $file)) {
			return true;
		}
	}

	/**
	 * 写入专题数据操作
	 * @param array $data [description]
	 */
	Public function CheckUPData($data = array()){
		if (empty($data['date']) && empty($data['time'])) {
			$date = date('Y.m.d');
			$time = date('H:i:s', time());
		} else {
			$date = $data['date'];
			$time = $data['time'];
		}
		if (empty($data['id'])) {
			$data['id'] = randCode(6);
			if (file_exists(C('page.data').$data['id'].'.Mark')) {
				$data['id'] = randCode(6);
			}
		}
		$data['content'] = $_POST['content'];
		$data['date'] = $date;
		$data['time'] = $time;
		$str = Getstr(htmlspecialchars_decode($data['content']), 300);
		if (empty($data['des'])) {
			$data['des'] = strip_tags($str);
		}
		if (empty($data['keyword'])) {
			$data['keyword'] = Getstr($str, 20);
		}
		$pagefile = loadfile(C('page.id'));
		$lastarr[$data['id']] = array(
			'date' => $date,
			'time' => $time,
			'ord' => $data['ord'],
			'id' => $data['id'],
			);
		$putarray = array_merge($pagefile, $lastarr);
		uasort($putarray, 'List_Sort');
		$hitsfiles = loadfile(C('hits'));
		$gethitarray = array();
		$gethitarray[$data['id']] = 100;
		$lasthits = array_merge($gethitarray, $hitsfiles);
		uasort($lasthits, 'hits_sort');
		varfile(C('hits'), $lasthits);
		if (varfile(C('page.id'), $putarray)) {
			if (serfile(C('page.data').$data['id'].'.Mark', $data)) {
				return true;
			}
		}
	}

	/**
	 * 删除专题数据
	 * @param  boolean $id [description]
	 * @return [type]      [description]
	 */
	Public function delPage($id = false){
		$file = loadfile(C('page.id'));
		unset($file[$id]);
		uasort($file, 'Post_Sort');
		$hits = loadfile(C('hits'));
		$pluanfileid = loadfile(C('pinglun.id').$id.'.php');
		unset($hits[$id]);
		uasort($hits, 'hits_sort');
		if (!empty($pluanfileid)) {
			foreach ($pluanfileid as $k => $v) {
				unlink(C('pinglun.data').$k.'.Mark');
			}
		}
		if (varfile(C('page.id'), $file) && unlink(C('page.data').$id.'.Mark') && varfile(C('hits'), $hits)) {
			$delpfile = C('pinglun.id').$id.'.php';
			if (file_exists($delpfile)) {
				unlink($delpfile);
			}
			return true;
		}
	}
}